package com.behome.netty.netty_D.bean;


import com.behome.common.utils.StringUtils;
import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.netty_D.enums.TerminalDAnnotation;
import com.behome.netty.netty_D.enums.TerminalDType;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;


@TerminalDAnnotation(
        type = TerminalDType.BROADCAST_IPHONE_REPORT
)
@Slf4j
public class Report_IPhone extends BusinessCmdBase {
    private Integer backCode;
    private Integer backData_Len;

    private String ebmId;
    private String protocol;
    private String pid;
    private String address;
    private String backDataHead = "distribution:";

    public Integer getBackCode() {
        return backCode;
    }

    public void setBackCode(Integer backCode) {
        this.backCode = backCode;
    }

    public Integer getBackData_Len() {
        return backData_Len;
    }

    public void setBackData_Len(Integer backData_Len) {
        this.backData_Len = backData_Len;
    }

    public String getEbmId() {
        return ebmId;
    }

    public void setEbmId(String ebmId) {
        this.ebmId = ebmId;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBackDataHead() {
        return backDataHead;
    }

    public void setBackDataHead(String backDataHead) {
        this.backDataHead = backDataHead;
    }

    public Report_IPhone() {
    }


    public byte[] serialize() {
        byte[] ret = null;
        int bodyLength = 4 + 4;
        String resultUrl = "";
        try{
            if (StringUtils.isNotEmpty(this.protocol) && this.protocol.equalsIgnoreCase("RTSP")) {
                resultUrl += this.backDataHead;
                resultUrl += this.protocol;
                resultUrl += ",";
                resultUrl += this.address;
                resultUrl += ",";
                resultUrl += "ebmid=";
                resultUrl += this.ebmId.contains("f") || this.ebmId.contains("F") ?
                        this.ebmId.replaceAll("f", "").replaceAll("F", "") : this.ebmId;
            }
            byte[] resultUrlBytes;
            if(!StringUtils.isEmpty(resultUrl)){
                resultUrlBytes = resultUrl.getBytes(StandardCharsets.US_ASCII);
            }else{
                resultUrlBytes = new byte[0];
            }
            this.backData_Len = resultUrlBytes.length;
            ByteBuffer byteBuffer = ByteBuffer.allocate(bodyLength + backData_Len);
            byteBuffer.put(ByteConventerKit.intToBytes2(this.backCode))
                    .put(ByteConventerKit.intToBytes2(this.backData_Len))
                    .put(resultUrlBytes);
            ret = byteBuffer.array();
        }catch (Exception e){
            e.printStackTrace();
        }

        return ret;
    }

    public boolean validateBeforeSerialize() {
        return true;
    }

    public BusinessCmdBase deserialize(byte[] busiData) {
        return this;
    }
}
